#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.Caching;

namespace Web.Enhancements.Rest
{
    /// <summary>
    /// Represents an attribute that is used to mark a REST-service method whose output will be cached.
    /// </summary>
    [Serializable()]
    [AttributeUsage(AttributeTargets.Method)]
    public class WebRestCacheAttribute : Attribute
    {
        /// <summary>
        /// Gets or sets the cache duration, in seconds.
        /// </summary>
        public int Duration { get; set; }
        /// <summary>
        /// Gets or sets a boolean flag indicating if the cache is enabled.
        /// </summary>
        public bool Enabled { get; set; }
        /// <summary>
        /// Gets or sets the location.
        /// </summary>
        public OutputCacheLocation Location { get; set; }
        /// <summary>
        /// Gets or sets the vary-by-parameter value. 
        /// You may use "None" to indicate no parameters, and "All" to include all parameters.
        /// </summary>
        public string VaryByParam { get; set; }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public WebRestCacheAttribute()
            : base()
        {
            this.Location = OutputCacheLocation.Server;
            this.VaryByParam = "None";
            this.Enabled = true;
        }

        public override string ToString()
        {
            if (this.Enabled)
                return string.Format("Cache: Duration: {1} sec; Location: {2}; VaryByParam: {3}.", this.GetType().Name, this.Duration.ToString(), this.Location.ToString(), this.VaryByParam);
            else
                return "Cache: Disabled";
        }
    }

    [Serializable()]
    internal class WebRestCacheParameters
    {
        public int Duration { get; set; }
        public bool Enabled { get; set; }
        public OutputCacheLocation Location { get; set; }
        public string VaryByParam { get; set; }
        public List<string> VaryByParamItems { get; private set; }

        public WebRestCacheParameters()
        {
            this.Location = OutputCacheLocation.Server;
            this.VaryByParam = "None";
            this.Enabled = true;
            this.VaryByParamItems = new List<string>();
        }
    }
}
